import router from '@/router'
import store from '@/store'
router.beforeEach(async(to, from, next) => {
  const token = store.getters.token
  //   if (token) {
  //     next()
  //   } else {
  //     if (to.path === '/login') {
  //       next()
  //     } else {
  //       next('/login')
  //     }
  //   }
  if (token && to.path === '/login') {
    next('/')
  }
  if (token && to.path !== '/login') {
    if (!store.state.user.userinfo.username) {
      await store.dispatch('user/getuserinfo')
      const res = await store.dispatch('permission/fn', store.state.user.userinfo.roles.menus)
      // store.dispatch('permission/fn', store.state.user.userinfo.roles.menus)
      console.log(res)
      router.addRoutes([...res,
        { path: '*', redirect: '/404', hidden: true }])
      next(to.path)
    } else {
      next()
    }
  }
  if (!token && ['/login', '/404'].indexOf(to.path) !== -1) {
    next()
  }
  if (!token && ['/login', '/404'].indexOf(to.path) === -1) {
    next('/login')
  }
})
